package cn.honqun.tinycms.constants;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.StrUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class ContentTypes {
    public static final String TYPE_DEFAULT = "application/octet-stream";
    public static final String TYPE_DOC = "application/msword";
    public static final String TYPE_DOCX = "application/msword";
    public static final String TYPE_XLS = "application/vndtype.ms-excel";
    public static final String TYPE_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    public static final String TYPE_PPT = "type.ppt=application/x-ppt";
    public static final String TYPE_PPTX = "type.ppt=application/x-ppt";
    public static final String TYPE_PNG = "image/png";
    public static final String TYPE_JPEG = "image/jpeg";
    public static final String TYPE_JPG = "image/jpeg";
    public static final String TYPE_GIF = "image/gif";
    public static final String TYPE_XHTML = "text/html";
    public static final String TYPE_HTML = "text/html";
    public static final String TYPE_HTM = "text/html";
    public static final String TYPE_XML = "text/xml";
    public static final String TYPE_CSV = "application/csv";
    public static final String TYPE_STREAM = "application/octet-stream";
    private static final Map<String, String> CONTENT_TYPES = new ConcurrentHashMap<>();

    static {
        CONTENT_TYPES.put("doc", "application/msword");
        CONTENT_TYPES.put("docx", "application/msword");
        CONTENT_TYPES.put("xls", "application/vndtype.ms-excel");
        CONTENT_TYPES.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        CONTENT_TYPES.put("ppt", "type.ppt=application/x-ppt");
        CONTENT_TYPES.put("pptx", "type.ppt=application/x-ppt");
        CONTENT_TYPES.put("xhtml", "text/html");
        CONTENT_TYPES.put("html", "text/html");
        CONTENT_TYPES.put("htm", "text/html");
        CONTENT_TYPES.put("xml", "text/xml");
        CONTENT_TYPES.put("csv", "application/csv");
        CONTENT_TYPES.put("jpg", "image/jpeg");
        CONTENT_TYPES.put("jpeg", "image/jpeg");
        CONTENT_TYPES.put("gif", "image/gif");
        CONTENT_TYPES.put("png", "image/png");
        CONTENT_TYPES.put("mp4", "video/mpeg4");
        CONTENT_TYPES.put("avi", "video/avi");
    }

    public ContentTypes() {
    }

    public static String get(String key) {
        if (StrUtil.isNotEmpty(key)) {
            return CONTENT_TYPES.getOrDefault(key.toLowerCase(), TYPE_STREAM);
        } else {
            return TYPE_STREAM;
        }
    }

    public static String getByName(String name) {
        String extName = FileNameUtil.extName(name);
        return extName != null ? get(extName) : TYPE_STREAM;
    }

}
